<?php
/**
 * User: b179a9
 * Date: 10/4/11
 * Time: 9:43 PM
 */

class IrubricsController extends Zend_Controller_Action
{

    const RUBRICS_PER_PAGE = 10;

    /**
     * FlashMessenger
     *
     * @var Lib_Flash
     */
    protected $_flashMessenger = null;

    public function init()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
        } else {
            $this->_helper->layout()->setLayout('admin-layout');
        }
        $this->_flashMessenger = new Lib_Flash();
    }

    public function indexAction()
    {
        $manager = new \Managers\ItemsRubrics();
        $rubrics = $manager->getList(null, true);

        $this->view->rubrics = $rubrics;
    }

    public function editAction()
    {
        $id = $this->_request->getParam('id');

        $manager = new \Managers\ItemsRubrics();
        $rubric = $manager->getById($id);

        if (!$rubric) {
            die('not exists');
        }

        $form = new App_Form_Irubric_Form();
        $form->populate($rubric);

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            if ($form->isValid($params)) {
                try {
                    $manager->save($params);
                    $this->_flashMessenger->addMessage('Rubrics successfully updated.', Lib_Flash::NOTICE, Lib_Flash::LATER);
                    $this->_redirect('/irubrics');
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                }
            }
        }

        $this->view->form = $form->render();
    }

    public function addAction()
    {
        $form = new App_Form_Irubric_Form();

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            if ($form->isValid($params)) {
                try {
                    $manager = new \Managers\ItemsRubrics();
                    $manager->save($params);
                    $this->_flashMessenger->addMessage('Rubrics successfully created.', Lib_Flash::NOTICE, Lib_Flash::LATER);
                    $this->_redirect('/irubrics');
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                }
            }

        }
        $this->view->form = $form->render();
    }

    public function dndAction()
    {

        if (!$this->_request->isXmlHttpRequest()) {
            $this->_redirect('/irubrics');
        }

        if ($this->_request->isPost()) {
            $operation = $this->_request->getParam('operation');
            if ('move_node' == $operation) {
                $id = $this->_request->getParam('id');
                $ref = $this->_request->getParam('ref');
                $pagesManager = new \Managers\ItemsRubrics();
                $result = $pagesManager->changeParent($id, $ref);

                $data = array('status' => $result, 'id' => $id);
                $this->view->data = $data;
                $this->renderScript('/ajax/json.phtml');
            }
        }

    }
}
